package kr.co.exight.onApp;

import library.ActionClass;
import library.ExPreferenceManager;
import library.JsonParser;
import library.Library;
import library.Variables;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class ActivityDelivery extends Activity implements OnCheckedChangeListener, OnClickListener {

	JSONObject jsonResult;
	TestAdapter adapter;
	ListView lstView;
	ExPreferenceManager pm;
	Button btnToggle;
	RadioGroup radioGroup;
	RadioButton btnKorea;
	boolean isYICMode = false;
	
	private final static String headerYicDelivery = "yic_";
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_baedal);
		radioGroup = (RadioGroup)findViewById(R.id.delivery_radioGroup);
		
		pm = new ExPreferenceManager(this);
		
		
		lstView = (ListView)findViewById(R.id.delivery_lstList);
		btnToggle = (Button)findViewById(R.id.delivery_btnToggle);
		btnToggle.setOnClickListener(this);
		
		
		TextView tvNone = (TextView)findViewById(R.id.delivery_tvNone);
		// TODO Auto-generated method stub
		Library.setFontNanumGothic(tvNone);
		Library.setTextViewBold(tvNone);

		
		if(pm.getItem("MODE_YIC").equals("true")){
			btnToggle.setText("신촌");
			isYICMode = true;
		}
		
		radioGroup.setOnCheckedChangeListener(this);
		btnKorea = (RadioButton)findViewById(R.id.delivery_btnKorean);
		btnKorea.setChecked(true);
		
	
	}
	
	

	public void onClick(View v) {
		if(btnToggle.getText().equals("신촌")){
			pm.putItem("MODE_YIC", "");
			btnToggle.setText("국캠");
			
			isYICMode = false;
			
			btnKorea.performClick();
			onCheckedChanged(radioGroup, R.id.delivery_btnKorean);
			
		}else if(btnToggle.getText().equals("국캠")){
			pm.putItem("MODE_YIC", "true");
			btnToggle.setText("신촌");
			isYICMode = true;
			
			btnKorea.performClick();
			onCheckedChanged(radioGroup, R.id.delivery_btnKorean);
		}
		
		
	}

	class TestAdapter extends BaseAdapter{

		public int getCount() {

			try {
				return jsonResult.getJSONArray("spots").length();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		}

		public Object getItem(int position) {

			try {
				return jsonResult.getJSONArray("spots").get(position);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return position;
		}

		public long getItemId(int position) {

			return 0;
		}

		public View getView(final int position, View convertView, ViewGroup parent) {

			View v = convertView;
			if(v == null){
				v = getLayoutInflater().inflate(R.layout.cell_deliverylist, null);
			}

			final JSONObject a;
			try {
				a = jsonResult.getJSONArray("spots").getJSONObject(position);


				TextView tvName = (TextView)v.findViewById(R.id.deliveryCell_tvName);
				Button btnTel = (Button)v.findViewById(R.id.deliveryCell_btnTel);
				LinearLayout llList = (LinearLayout)v.findViewById(R.id.deliveryCell_ll);
				
				Library.setFontNanumGothic(tvName);
				Library.setTextViewBold(tvName);

				tvName.setText(a.getString("name"));
				
				btnTel.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						// TODO Auto-generated method stub
						try {
							Library.openPhoneCallIntent(ActivityDelivery.this, a.getJSONArray("phone").getString(0));
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					}
				});
				
				llList.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						// TODO Auto-generated method stub
						ActivityDeliveryDetail.setJSONData(a);
						Library.startActivity(ActivityDelivery.this, ActivityDeliveryDetail.class);
					}
				});
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return v;
		}
	}



	public void setDataFromJSON(final String category){
		showDialog(0);
		
		
		JsonParser parser = new JsonParser(Variables.SERVER_URL + "/delivery/list/" + category, 
				new ActionClass(ActivityDelivery.this) {
			@Override
			public void actionSuccess(Object object) {
				// TODO Auto-generated method stub
				try{
					dismissDialog(0);
				}catch(IllegalArgumentException e){
					e.printStackTrace();
				}
				
				jsonResult  = (JSONObject)object;
				
				
				if(adapter == null){
					adapter = new TestAdapter();
					lstView.setAdapter(adapter);
					
				}else {
					adapter.notifyDataSetChanged();
				}
				
				try {
					if(jsonResult.getInt("counts") != 0) 
						lstView.setVisibility(View.VISIBLE);
					else{
						lstView.setVisibility(View.GONE);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}


			}
			@Override
			public void actionFailed() {
				// TODO Auto-generated method stub
				try{
					dismissDialog(0);
				}catch(IllegalArgumentException e){
					e.printStackTrace();
				}
				super.actionFailed();
			}

		});
		parser.start();
	}

	@Override // 로딩바 안보이게 하기
	protected Dialog onCreateDialog(int id) {
		final Dialog dialog = new Dialog(this, R.style.Theme_Dialog);
		View view = getLayoutInflater().inflate(R.layout.dialog_progress, null);
		dialog.setContentView(view);
		dialog.setOnCancelListener(new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		return dialog;

	}


	public void onCheckedChanged(RadioGroup arg0, int arg1) {
		StringBuilder category = new StringBuilder();
		if(isYICMode) category.append(headerYicDelivery); 
		switch(arg1){
		case R.id.delivery_btnKorean:
			setDataFromJSON(category.append("kfood").toString());
			break;
		case R.id.delivery_btnChinese:
			setDataFromJSON(category.append("cfood").toString());
			break;
		case R.id.delivery_btnChicken:
			setDataFromJSON(category.append("chicken").toString());
			break;
		case R.id.delivery_btnPizza:
			setDataFromJSON(category.append("pizza").toString());
			break;
		case R.id.delivery_btnEtc:
			setDataFromJSON(category.append("etc").toString());
			break;
		}
	}
}
